'use strict';
/*commonDomDirective*/
/*模态框显示隐藏指令*/
yjx.directive('modalDir', ['$rootScope', '$timeout', function modalDir($rootScope, $timeout) {
    return {
        replace: false,
        restrict: 'EAC',
        scope: {
            modalDir: '=modalDir', // 绑定对象
            modalTag: '@modalTag', //指定标签别名
            clickHide: '=clickHide', //是否点击外部隐藏 适用单层，多层模态框组有bug（如支付）
            clickCg: '=clickCg', // 点击外部 改变对象
            modalBg:'@modalBg',
        },
        link: function ($scope, $element, attr) {
            $scope.$watch('modalDir', function (newVal) {
                $scope.modalBg = $scope.modalBg?$scope.modalBg:'.modal-bg';

                var e = $element.closest($scope.modalBg);
                if (e.length == 0) {
                    e = $element;
                }
                //alert( $scope.modalTag);
                var modalTag = $scope.modalTag;
                //close关闭指定tag
                var close = e.find('.e-close');
                //open打开指定tag
                var open = e.find('.open');
                //点击外层改变状态
                e.bind('click', function (event) {
                    console.log($(event.target).context.className);
                    console.info(($(event.target).context.className).search('modal-bg')>=0);
                    console.log($scope.clickHide)
                    if ($scope.clickHide && $(event.target).context.className.search('modal-bg') >= 0) {
                        $timeout(function () {
                            console.log( 'modal-hide');
                            $rootScope.modalStatus = 'modal-hide';
                            $rootScope.modalTag = modalTag;
                            if ($scope.clickCg) {
                                $scope.clickCg = !$scope.clickCg;// 点击外部 改变对象
                            }
                        });
                    }
                });
                //close关闭指定tag改变状态
                close.bind('click', function () {
                    $timeout(function () {
                        $rootScope.modalStatus = 'modal-hide';
                        $rootScope.modalTag = modalTag;

                    });
                });
                //open打开指定tag改变状态
                open.bind('click', function () {
                    $timeout(function () {
                        $rootScope.modalStatus = 'modal-show';
                        $rootScope.modalTag = modalTag;
                    });
                });
            });
        }
    }
}]);
yjx.directive('activeUli', ['$timeout', function activeUli($timeout) {
    //适用于 ul>li.active 结构 置于ul标签
    return {
        restrict: 'EAC',
        priority: 1,
        scope: true,
        link: function ($scope, $element, attr) {
            $scope.$watch(attr.activeUli, function () {
                //对于li为遍历数据，可能会无法计时获得li数据（请求时间差），监听遍历对象是否准备完毕
                var li = $element.find(">li[uli-not-works!='true']");
                if (attr.activeAuto) {
                    if (attr.activeEventend) {
                        li.first().addClass('active').css('pointer-events', 'none');
                    } else {
                        li.first().addClass('active');
                    }
                }
                //console.log(li);
                li.bind('click', function (event) {
                    //console.log('click');
                    if (attr.activeEventend) {
                        $element.find('>li').removeClass('active').css('pointer-events', 'all');
                        $(this).addClass('active').css('pointer-events', 'none');
                    } else {
                        $element.find('>li').removeClass('active');
                        $(this).addClass('active');
                    }
                });
                //对为过滤的li添加 效果
                //console.info((li['uli-not-works'==true]).attr('uli-not-works'))
                //$element.find("li[uli-not-works!=true]").bind('mouseleave', function (event) {
                //    $(this).removeClass('active');
                //});
            },true)
        }
    }
}]);
yjx.directive('touchCgHide', function touchCgHide() {//触屏上下滑动时 添加移除touch-hide类名 依赖于外部scope中已有containerScroll指令
    return {
        restrict: 'AEC',
        scope: false,
        link: function ($scope, elem, attr) {
            //监听xy轴上的触摸
            $scope.$watch(attr.touchCgHide, function () {
                //当向下滑动超过10px时(_yy<-10),隐藏：添加touch-hide类名
                if ($scope._yy < -10 && (elem.attr('class') + '').search('touch-hide') < 0) {
                    elem.addClass('touch-hide')
                }
                //当向上滑动超过10px时(_yy>10),显示：移除touch-hide类名
                if ($scope._yy > 10) {
                    elem.removeClass('touch-hide')
                }
            })
        }
    }
})
/**点击后无法立即点击**/
yjx.directive('pointerDisable', ['$timeout', function pointerDisable($timeout) {
    return {
        restrict: 'A',
        priority: 99,
        scope: true,
        link: function ($scope, elem, attr) {
            var expire = attr.clickDisable ? attr.clickDisable : 3; //超时时间/秒
            elem.bind('click', function () {
                elem.addClass('pointer-none');
                $timeout(function () {
                    elem.removeClass('pointer-none');
                }, expire * 1000)
            })
        }
    }
}])

//下拉选框展示隐藏
yjx.directive('selectingHide', ['$rootScope', function ($rootScope) {
    return {
        restrict: 'EAC',
        scope: false,
        link: function ($scope, $element, attr) {
            var p = $element.parent().parent();
            var c = $element.find('li');
            //console.info(p);
            //console.info(c);
            var stpure = ($element.attr('class') + '').search('style-pure') != -1;//纯净样式 点击只显示一个框
            $element.bind('touch', function () {
                //console.info($element);
                //鼠标点击区域为下拉选择框
                $rootScope.md = 1;
                if ($element.find('ul').attr('class') == 'e-hide') {
                    //more按钮 隐藏
                    angular.element(document.querySelector('body')).find('.more').removeClass('moreinfo');
                    //下拉选框展示隐藏
                    angular.element(document.querySelector('body')).find('.e-show').addClass('e-hide');
                    angular.element(document.querySelector('body')).find('.e-show').removeClass('e-show');
                    if (stpure) {
                        $element.addClass('e-show');
                        $element.removeClass('e-hide');
                    }
                    $element.find('ul').addClass('e-show');
                    $element.find('ul').removeClass('e-hide');
                } else {
                    if (stpure) {
                        $element.addClass('e-hide');
                        $element.removeClass('e-show');
                    }
                    $element.find('ul').addClass('e-hide');
                    $element.find('ul').removeClass('e-show');
                }
                return;
            });
        }
    }
}]);
    //外层点击隐藏
yjx.directive('ehideShow', ['$rootScope', '$timeout', function ($rootScope, $timeout) {
        return {
            restrict: 'EA',
            link: function ($scope, $element, attr) {
                //初始化鼠标点击区域 0外部 1下拉选择框
                $rootScope.appModalStatus = true;
                $rootScope.md = 0;
                $element.bind('touch', function () {
                    if ($rootScope.md == 0) {
                        //若为外部则隐藏下拉选择框
                        $element.find('.e-show').addClass('e-hide');
                        $element.find('.e-show').removeClass('e-show');
                        //隐藏更多展示
                        $element.find('.more').removeClass('moreinfo');
                        $('.charts_tooltip').remove();
                    } else {
                        //重置鼠标点击区域 0外部
                        $rootScope.md = 0;
                    }
                });
            }
        }
    }]);

/*监听resize*/
